package id.co.smltech.servo.project.ejb;
import id.co.smltech.servo.project.ejb.dao.ActivityTrackingDao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.core.Response;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


@Stateless
@LocalBean
public class ActivityTrackingAdministration {

	@EJB ActivityTrackingDao activityTrackingDao;
	
	public Response downloadTemplate() {
		
		String outputFileName = "tmp_activity_tracking.xls";
		File outputFile = new File("D:\\upload\\export_link_ali\\"+outputFileName);

		try {
			FileInputStream input_document = new FileInputStream(new File("D:\\Work\\Activity Tracking\\activity_tracking.xls"));
			HSSFWorkbook workbook = new HSSFWorkbook(input_document);
			HSSFSheet my_worksheet = workbook.getSheetAt(0);
			FileOutputStream out = new FileOutputStream(outputFile);
			workbook.write(out);
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		if (outputFile.exists()) {

			Date date = new Date();
			DateFormat dateTimeFormatForDownloadName = new SimpleDateFormat("yyyyMMddHHmmss");
			String responseFileName = dateTimeFormatForDownloadName.format(date)+"activity_tracking.xls";
			return Response.ok(outputFile).header("Content-Disposition", "attachment; filename="+responseFileName).build();

		} else {
			return Response.status(500).entity("Error when create download link").build();
		}
	}
	
	 public void uploadFileExcelSims(){
		 activityTrackingDao.uploadFileExcelActivityTracking();
	    }
}
